package com.goodfox.web.generic.authority.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.goodfox.web.generic.authority.entity.Resources;
import com.goodfox.web.generic.authority.mapper.ResourcesMapper;
import com.goodfox.web.generic.authority.service.ResourcesService;
import com.goodfox.web.generic.provide.annotation.NoteExplain;


/**
 * 资源Service实现类
 * @author qin gt
 *
 */
@Transactional
@Service(value="resourcesService")
@NoteExplain(comment="资源管理")
public class ResourcesServiceImpl implements ResourcesService {

	/** log4j日志器 */
	private Logger logger=Logger.getLogger(ResourcesServiceImpl.class);
	
	/** 资源Mapper */
	@Autowired
	private ResourcesMapper resourcesMapper;

	@NoteExplain(comment="查询资源列表")
	public List<Resources> queryResourcesList(){
		Map map=new HashMap();
		map.put("resState", "1");
		List<Resources> rseList=resourcesMapper.selectResourcesList(map);
        for(Resources rse:rseList){
        	try{
        		rse.setRoleList(resourcesMapper.selectRoleListByRseId(rse.getRseId()));
        	}catch(Exception ex){
        		logger.error("通过资源Id获取角色List异常",ex);
        	}
        }
		return rseList;
	}
	
	@NoteExplain(comment="查询资源集合")
	public Set<Resources> queryResourcesSetByRoleId(String roleId) {
		Set<Resources> resourceSet=new HashSet<Resources>();
        for(Resources resources:resourcesMapper.selectResourcesListByRoleId(roleId)){
        	resourceSet.add(resources);
        }
		return resourceSet;
	}

	@NoteExplain(comment="查询资源列表")
	public List<Resources> queryResourcesListByFunId(String funId){
		Map map=new HashMap();
		map.put("funId", funId);
		return resourcesMapper.selectResourcesList(map);
	}

	@NoteExplain(comment="获取资源")
	public Resources getResources(String rseId){
		return resourcesMapper.selectResourcesById(rseId);
	}

	@NoteExplain(comment="新增资源",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void addResources(Resources entity,String loginLogId){
		entity.setRseState("1");
		resourcesMapper.insertResources(entity);
	}

	@NoteExplain(comment="更新资源",paraDesc="oldEny 旧实体;newEny 新实体;loginLogId 登录日志Id")
	public void modifyResources(Resources oldEny,Resources newEny,String loginLogId){
		resourcesMapper.updateResources(newEny);
	}

	@NoteExplain(comment="移除资源",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void removeResources(Resources entity,String loginLogId){
		resourcesMapper.deleteResourcesById(entity.getRseId());
	}
}
